package com.javaee.term.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javaee.term.Enums.LogistStatusEnum;
import com.javaee.term.exception.HandException;
import com.javaee.term.form.CustomerForm;
import com.javaee.term.form.LogistInfoFrom;
import com.javaee.term.model.Customer;
import com.javaee.term.model.LogistInfo;
import com.javaee.term.service.LogistInfoService;
import com.javaee.term.utils.KeyUtil;
import javassist.expr.NewArray;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.mapping.Array;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * author 陈伯瑞
 * date 2019/12/21 0021 13:02
 * version 1.0
 **/
@Controller
@RequestMapping("/seller/logist")
@Slf4j
public class LogistController {
    @Autowired
    LogistInfoService logistInfoService;

    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map) {

        PageHelper.startPage(page,size);
        List<LogistInfo> list=logistInfoService.findAll();
        PageInfo<LogistInfo> LogistInfoPage=new PageInfo<LogistInfo>(list);
        map.put("LogistInfoPage", LogistInfoPage);
        map.put("currentPage", page);
        map.put("size", size);
        map.put("totalpage", LogistInfoPage.getPages());
        return new ModelAndView("logist/list", map);
    }
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "logistId", required = false) String logistId,
                              @RequestParam(value = "sendId", required = false) String sendId,
                              Map<String, Object> map) {
        if (!StringUtils.isEmpty(logistId)) {
            LogistInfo  logistInfo = logistInfoService.findOne(logistId);
            map.put("logistInfo", logistInfo);
            return new ModelAndView("logist/index", map);
        }else {
            LogistInfo bySendId = logistInfoService.findBySendId(sendId);
            if(bySendId==null) {
                LogistInfo logistInfo = new LogistInfo();
                logistInfo.setSendId(sendId);
                logistInfo.setCreateTime(new Date());
                logistInfo.setLogistStatus(LogistStatusEnum.COLLECTED.getCode());
                map.put("logistInfo", logistInfo);
            }else
                map.put("logistInfo", bySendId);
            return new ModelAndView("logist/index", map);
        }
    }
    @PostMapping("/save")
    public ModelAndView save(@Valid LogistInfoFrom form,
                             BindingResult bindingResult,
                             Map<String, Object> map) {
        if (bindingResult.hasErrors()) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/sell/seller/logist/list");
            return new ModelAndView("common/error", map);
        }

        LogistInfo logistInfo = new LogistInfo();
        try {

            if (!StringUtils.isEmpty(form.getLogistId())) {
                logistInfo = logistInfoService.findOne(form.getLogistId());
                BeanUtils.copyProperties(form, logistInfo);
                logistInfoService.update(logistInfo);
            } else {
                form.setLogistId(KeyUtil.genUniqueKey());
                BeanUtils.copyProperties(form, logistInfo);
                logistInfoService.insert(logistInfo);
            }
        } catch (HandException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/logist/list");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/sell/seller/logist/list");
        return new ModelAndView("common/success", map);
    }
}
